<html xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>BeginCode后台管理系统-博文管理</title>
    <link rel="stylesheet" href="${rc.contextPath}/layui/css/layui.css" media="all">
    <script src="${rc.contextPath}/layui/layui.all.js"></script>
    <script src="${rc.contextPath}/js/jquery.js"></script>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
<#include "common/top.ftl">
<#include "common/left.ftl">
    <div class="layui-body">

        <div class="layui-form" style="padding-top: 30px">

            <div class="layui-form-item">

                <div class="layui-inline">
                    <label class="layui-form-label">博文状态</label>
                    <div class="layui-input-block">
                        <select name="blogFlag" id="blogFlag" lay-filter="blogFlag">
                            <option value="CREATE">创建</option>
                            <option value="PUBLIC" selected="">已发布</option>
                            <option value="DELETE">删除</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">审核状态</label>
                    <div class="layui-input-block">
                        <select name="checkFlag" id="checkFlag" lay-filter="checkFlag">
                            <option value="INIT" selected="">未审核</option>
                            <option value="PASS">通过</option>
                            <option value="FAIL">未通过</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" id="query" lay-filter="userQuery">查询</button>
                    </div>
                </div>
            </div>
        </div>

        <table id="blogList" lay-filter="opt"></table>
    </div>
<#include "common/foot.ftl">
</div>
</body>
</html>
<div id="layer-keyword-form" style="display:none;">
    <div class="layui-form" style="padding-top: 30px">
        <input type="hidden" id="blogId" name="blogId">
        <div id="keyWordList">
            <!-- 关键字列表-->
        </div>
        <div>
            <button class="layui-btn" id="keyWordSave" class="layui-btn">保存</button>
        </div>
    </div>
</div>


<script type="text/html" id="optTools">
    <a class="layui-btn layui-btn-xs" lay-event="pass">通过</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="failed">不通过</a>
</script>

<script type="text/html" id="keyTools">
    <a class="layui-btn layui-btn-xs" lay-event="keys">关键字</a>
</script>

<script type="text/html" id="homeTypeTools">
    <a class="layui-btn layui-btn-xs" lay-event="homeType">系统分类</a>
</script>

<script type="text/html" id="usernameTpl">
    <a href="" class="layui-table-link">{{d.username || ''}}</a>
</script>


<script src="${rc.contextPath}/layui/layui.all.js" charset="utf-8"></script>
<script>
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        var jquery = layui.$;
        //渲染
        table.render({
            id: 'blogReload'
            , elem: '#blogList'
            , height: 'full-200'
            , url: '${rc.contextPath}/admin/blog/queryWaitCheckBlog'
            , cellMinWidth: 80
            , page: true
            , request: {
                pageName: 'pageNo' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , where: {
                checkFlag: jquery("#checkFlag").val(),
                blogFlag: jquery("#blogFlag").val()
            }
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', fixed: 'left', unresize: true}
                , {field: 'blogTitle', title: '标题'}
                , {field: "cdate", title: '创建时间'}
                , {field: 'checkFlag', title: '状态'}
                , {field: 'keyWords', title: '关键字'}
                , {title: '分配关键字', toolbar: '#keyTools'}
                , {title: '分配系统分类', toolbar: '#homeTypeTools'}
                , {fixed: 'right', title: '操作', toolbar: '#optTools'}

            ]]

        });

        //监听工具条
        table.on('tool(opt)', function (obj) {
            var data = obj.data;
            var url;
            if (obj.event === 'pass') {
                url = "${rc.contextPath}/admin/blog/checkBlogPass";
            } else if (obj.event === 'failed') {
                url = "${rc.contextPath}/admin/blog/checkBlogFail";
            } else if (obj.event === 'keys') {
                keyWordForm(data.id);
                return;
            } else if (obj.event === 'homeType') {
                layer.msg('分配系统分类', {
                    icon: 2,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                });
                layer.close(index);
            } else {
                layer.msg('非法操作', {
                    icon: 2,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                });
                layer.close(index);
            }
            jquery.post(
                    url
                    , {"blogId": data.id}
                    , function (result) {
                        if (result.success && result.data) {
                            layer.msg('操作成功', {
                                icon: 1,
                                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                            });
                        }
                        if (result.success) {
                            reloadTable();
                            layer.close(index);
                        }
                    }
            );
        });

        jquery('#query').on('click', function () {
            reloadTable();
        });


        jquery('#keyWordSave').on('click', function () {
            var standards = "";
            jquery("input:checkbox[name='blogKeyWords']:checked").each(function(i,item) { // 遍历name=standard的多选框
                if(i == 0){
                    standards = item.value
                }else{
                    standards += ',' +  item.value;
                }

            });
            var blogId = jquery("#blogId").val();
            <!-- 保存博文关键字 -->
            jquery.post(
                    "${rc.contextPath}/admin/blogKeyWord/setKeyWords"
                    , {"keyWordIds": standards,"blogId":blogId}
                    , function (result) {
                        if (result.success && result.data) {
                            layer.msg('操作成功', {
                                icon: 1,
                                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                            });
                            jquery("#keyWordList").empty();
                            layer.closeAll('page');
                        } else {
                            layer.msg('操作失败', {
                                icon: 1,
                                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                            });
                        }
                        reloadTable();
                    }
            );
        });

        function reloadTable() {
            table.reload('blogReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , request: {
                    pageName: 'pageNo' //页码的参数名称，默认：page
                    , limitName: 'pageSize' //每页数据量的参数名，默认：limit
                }
                , where: {
                    checkFlag: jquery("#checkFlag").val(),
                    blogFlag: jquery("#blogFlag").val()
                }
            });
        }

        function keyWordForm(blogId) {

            jquery.post(
                    "${rc.contextPath}/admin/keyWords/queryEffectKeyWords"
                    , {}
                    , function (data) {
                        if (data.success) {
                            jquery("#keyWordList").empty();
                            $.each(data.data, function (i, item) {
                                var keyWordStr = "<input type=\"checkbox\" value=\"" + item.id + "\" style=\"width: 200px;\" name=\"blogKeyWords\" title=\"" + item.keyWord + "\"  />";
                                jquery("#keyWordList").append(keyWordStr);
                            });
                            jquery("#blogId").val(blogId);
                            form.render("checkbox");
                        }
                    }
            );


            layer.open({
                area: ['500px', '500px']
                , type: 1
                , title: '配置关键字'
                , content: jquery('#layer-keyword-form')
                , anim: 5
            });
        }
    });
</script>